﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MLBService.Resources
{
    [DataContract]
    public class FieldingStatsItem
    {
        #region Properties

        [DataMember]
        public UriLink PlayerItem { get; set; }
        [DataMember]
        public List<UriLink> Teams = new List<UriLink>();
        [DataMember]
        public List<int?> Year = new List<int?>();
        [DataMember]
        public List<int?> Stint = new List<int?>();
        [DataMember]
        public List<int?> G = new List<int?>();
        [DataMember]
        public List<int?> GS = new List<int?>();
        [DataMember]
        public List<int?> IO = new List<int?>();
        [DataMember]
        public List<int?> PO = new List<int?>();
        [DataMember]
        public List<int?> A = new List<int?>();
        [DataMember]
        public List<int?> E = new List<int?>();
        [DataMember]
        public List<int?> DP = new List<int?>();
        [DataMember]
        public List<int?> PB = new List<int?>();
        [DataMember]
        public List<int?> WP = new List<int?>();
        [DataMember]
        public List<int?> SB = new List<int?>();
        [DataMember]
        public List<int?> CS = new List<int?>();
        [DataMember]
        public List<int?> PickOffs = new List<int?>();
        [DataMember]
        public List<string> Pos = new List<string>();

        #endregion

        public FieldingStatsItem Load(int playerid, int year)
        {
            using (Model.fourseamEntities context = new Model.fourseamEntities())
            {
                var s = (
                    from p in context.People
                    from a in context.Appearances
                    from ft in context.FieldedTeams
                    from nn in context.TeamNickNames
                    from fs in context.FieldingStats
                    where a.PersonId == p.PersonId &&
                        ft.FieldedTeamId == a.TeamId &&
                        nn.TeamNickNameId == ft.TeamNickNameId &&
                        fs.TeamId == ft.FieldedTeamId &&
                        p.PersonId == playerid &&
                        ft.Yr == year &&
                        fs.PersonId == p.PersonId
                    orderby fs.Stint
                    select new
                    {
                        p.FirstName,
                        p.LastName,
                        p.PersonId,
                        ft.Yr,
                        nn.NickName,
                        ft.FieldedTeamId,
                        fs.A,
                        fs.CS,
                        fs.DP,
                        fs.E,
                        fs.G,
                        fs.GS,
                        fs.InnOuts,
                        fs.Missing_G,
                        fs.Missing_G_C,
                        fs.PB,
                        fs.PickOffs,
                        fs.PO,
                        fs.POS,
                        fs.SB,
                        fs.Stint,
                        fs.WP,
                        fs.ZR
                    }).ToList();

                foreach (var itm in s)
                {
                    this.A.Add(itm.A);
                    this.CS.Add(itm.CS);
                    this.DP.Add(itm.DP);
                    this.E.Add(itm.E);
                    this.G.Add(itm.G);
                    this.GS.Add(itm.GS);
                    this.IO.Add(itm.InnOuts);
                    this.PB.Add(itm.PB);
                    this.PickOffs.Add(itm.PickOffs);
                    this.PO.Add(itm.PO);
                    this.SB.Add(itm.SB);
                    this.Stint.Add(itm.Stint);
                    this.WP.Add(itm.WP);
                    this.Year.Add(itm.Yr);
                    this.Pos.Add(itm.POS);
                    this.Teams.Add(new UriLink(String.Format("{0}team/{1}", Constants.baseUrl, itm.FieldedTeamId), string.Format("{0} {1}", itm.Yr, itm.NickName)));
                }

                var pi = (from p in context.People
                          where p.PersonId == playerid
                          select p).FirstOrDefault();
                this.PlayerItem = new UriLink(string.Format("{0}player/{1}", Constants.baseUrl, pi.PersonId), string.Format("{0} {1}", pi.FirstName, pi.LastName));


                return this;
            }
        }

    }
}